﻿using AutoMapper;
using BPMN.Application.Contracts.DTO.Defective;
using BPMN.Application.Contracts.DTO.Response;
using BPMN.Application.IService;
using BPMN.Domain.Entity.DefectiveModel;
using BPMN.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPMN.Application.Service
{
    public class DefectiveService : IDefectiveService
    {
        public DefectiveService(IRepositoryBase<Defective> defect, IMapper mapper)
        {
            Defect = defect;
            Mapper = mapper;
        }

        public IRepositoryBase<Defective> Defect { get; }
        public IMapper Mapper { get; }


        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="defective"></param>
        /// <returns></returns>
        public async Task<int> AddDefective(Defective defective)
        {
            defective.CreatorId = 1;
            defective.Updator = 1;
            return await Defect.Add(defective);
        }


        //获取不良品相所有数据
        public async Task<ResultResponseDto<List<DefectiveDto>>> responseDto(string BianHoa,string DefName)
        {
            var info = (await Defect.GetAll()).Where(x => x.IsDel == true).ToList();

            if (!string.IsNullOrEmpty(BianHoa))
            {

                info = info.Where(x => x.Defective_BianHao.Contains(BianHoa)).ToList(); 
            }
            if (!string.IsNullOrEmpty(DefName))
            {

                info = info.Where(x => x.Defective_Name.Contains(DefName)).ToList();
            }


            return new ResultResponseDto<List<DefectiveDto>>()
            {
                ErrorInfo = "获取成功",
                Code = DomianShare.ResultCode.Ok,
                Data = Mapper.Map<List<DefectiveDto>>(info),
            };
        }

        //批量删除
        public async Task<ResultResponseDto<int>> DeleteDefective(List<int> ids)
        {
            var info = (await Defect.GetAll()).ToList();


            foreach (var item in info)
            {
                foreach (var id in ids)
                {
                    if (item.Defective_Id == id)
                    {
                        item.IsDel = false;

                        await Defect.Update(item);
                    }
                }

            }
            return new ResultResponseDto<int>()
            {
                ErrorInfo = "删除成功",
                Code = DomianShare.ResultCode.Ok,

            };
        }


        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Defective> DefectiveFanTian(int id)
        {
            return (await Defect.GetAll()).FirstOrDefault(x => x.Defective_Id == id);

        }

        //修改
        public async Task<ResultResponseDto<int>> UpdDefective(Defective defective)
        {
            defective.UpdateDate = DateTime.Now;

            defective.IsDel = true;

            await Defect.Update(defective);

            return new ResultResponseDto<int>()
            {
                ErrorInfo = "修改成功",
                Code = DomianShare.ResultCode.Ok,
            };
        }
















    }
}
